package com.gobo.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gobo.eduservice.pojo.EduTeacher;
import com.gobo.eduservice.mapper.EduTeacherMapper;
import com.gobo.eduservice.pojo.vo.EduTeacherQuery;
import com.gobo.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author gobo-generator
 * @since 2021-09-16
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Override
    public void pageQuery(Page<EduTeacher> pageParam, EduTeacherQuery eduTeacherQuery) {

        if (eduTeacherQuery==null){
            baseMapper.selectPage(pageParam,null);
            return;
        }

        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        String name = eduTeacherQuery.getName();
        String begin = eduTeacherQuery.getBegin();
        Integer level = eduTeacherQuery.getLevel();
        String end = eduTeacherQuery.getEnd();

        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)){
            queryWrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)){
            queryWrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            queryWrapper.le("gmt_modified",end);
        }
        queryWrapper.orderByDesc("gmt_modified");
        baseMapper.selectPage(pageParam,queryWrapper);

    }

    @Cacheable(value = "teacher",key="'selectIndexFourTeacher'")
    @Override
    public List<EduTeacher> getIndexTeachers() {
        QueryWrapper<EduTeacher> wrapperTeacher = new QueryWrapper<>();
        wrapperTeacher.orderByAsc("level");
        wrapperTeacher.last("limit 4");
        return baseMapper.selectList(wrapperTeacher);
    }

    @Override
    public Map<String, Object> getFrontPageList(Page<EduTeacher> page) {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort");
        baseMapper.selectPage(page,wrapper);
        long total = page.getTotal();
        long pages = page.getPages();
        List<EduTeacher> records = page.getRecords();
        long size = page.getSize();
        long current = page.getCurrent();
        boolean hasNext = page.hasNext();
        boolean hasPrevious = page.hasPrevious();
        HashMap<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }
}
